//
//  WYOpportunityController.m
//  Boss
//
//  Created by admin on 2018/7/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYOpportunityController.h"
#import "WYFoodCell.h"
#import "WYBusinessColect.h"
#import "WYBusinessDetailController.h"
@interface WYOpportunityController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL isrefresh;
@end

@implementation WYOpportunityController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    
    [self setNoDataViweWithTitle:@"您还没有商机收藏哦～" img:[UIImage imageNamed:@"商机收藏_空"]];
    
    
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getCollectListSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.isrefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            [weakSelf.dataArray addObjectsFromArray:[WYBusinessColect getBusinessCollectFromJson:response[@"result"][@"detail"]]];
            if (weakSelf.dataArray.count) {
                [weakSelf hiddenNoDataView];
            }else{
                [weakSelf showNoDataView];
            }
            
            [weakSelf.tableView reloadData];
        }
        
        
        
        
    }];
    
    
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYFoodCell" bundle:nil] forCellReuseIdentifier:@"WYFoodCell"];
    self.navigationItem.title = @"商机收藏";
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 100;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYFoodCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYFoodCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WYBusinessColect *business = self.dataArray [indexPath.row];
    [cell setBusiness:business];
//    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYBusinessColect *business = self.dataArray [indexPath.row];
    WYBusinessDetailController *bdc =[[WYBusinessDetailController alloc]init];
    bdc.navTitle =business.businessName ;
    bdc.urlString = business.id;
    [self.navigationController pushViewController:bdc animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 108;
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
////修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消\n收藏";
}
//-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    WEAKSELF
//    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
//        title:@"取消\n收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        WYBusinessColect *business  = weakSelf.dataArray[indexPath.row];
//        [weakSelf cancelCollectWithID:business.id];
//        NSLog(@"跟进");
//
//    }];
//    rowAction.backgroundColor = kColorMainColor;
//
//    NSArray *arr = @[rowAction];
//    return arr;
//
//}




////设置进入编辑状态时，Cell不会缩进
//- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
//    return NO;
//}
//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
    WYBusinessColect *business  = self.dataArray[indexPath.row];
    [self cancelCollectWithID:business.id];
    //删除数据，和删除动画
   // [self.myDataArr removeObjectAtIndex:deleteRow];
//    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteRow inSection:0]] withRowAnimation:UITableViewRowAnimationTop];



}
-(void)cancelCollectWithID:(NSString *)ID
{
    WEAKSELF
    [WYHttpServer getCollectionWithOpportunityId:ID WithType:@"2" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            weakSelf.isrefresh = YES;
            [weakSelf loadData];
        }else{
            
        }
        
    }];
}




- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
